package net.kleinhenz.questor.model;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Commit;

import net.kleinhenz.questor.ui.WebStoryView;

@Root(name="content")
public class Content
{
	private static Pattern CHOICE_BASE_PATTERN = Pattern.compile("<choice[ ]+target=['\"]([0-9]+)['\"][^>]*>([^<]*)</choice>");
	private static Pattern CHOICE_STATEMENT_PATTERN = Pattern.compile("[ ]+statement=['\"]([^\"]+)['\"].*>");
	private static Pattern CHOICE_STATEMENTTYPE_PATTERN = Pattern.compile("[ ]+statementtype=['\"]([^\"]+)['\"].*>");
	
	@Attribute(required=false, name="condition")
	private String conditionText = null;
	
	@Attribute(required=false, name="conditiontype")
	private Executable.Type conditionType = null;
	
	@Element(data=true, required=true)
	private String qhtml;
	
	private Condition condition = null;
	private List<Choice> choices = new ArrayList<Choice>();
	
	public void setQhtml(String qhtml)
	{
		this.qhtml = qhtml;
	}

	public String getQhtml()
	{
		return qhtml;
	}

	public void setConditionText(String conditionText) {
		this.conditionText = conditionText;
	}

	public String getConditionText() {
		return conditionText;
	}

	public void setConditionTypeText(Executable.Type conditionType) {
		this.conditionType = conditionType;
	}

	public Executable.Type getConditionType() {
		return conditionType;
	}
	
	public void setCondition(Condition condition) {
		this.condition = condition;
	}

	public Condition getCondition() {
		return condition;
	}

	public void setChoices(List<Choice> choices) {
		this.choices = choices;
	}

	public List<Choice> getChoices() {
		return choices;
	}

	@Commit
	public void createCondition() {

		// retrieve condition if it exists
		if (getConditionType()!=null && getConditionText()!=null)
			setCondition(ConditionFactory.createCondition(getConditionType(), getConditionText()));

		// retrieve choices
        Matcher matcher = CHOICE_BASE_PATTERN.matcher(getQhtml());
        while (matcher.find()) {
        	int target = -1;
    		String text = null;
    		String statement = null;
    		String statementType = null;
            target = Integer.parseInt(matcher.group(1));
            text = matcher.group(2);
            if (matcher.group().contains(" statement=")) {
            	Matcher cMatcher1 = CHOICE_STATEMENT_PATTERN.matcher(matcher.group());
            	Matcher cMatcher2 = CHOICE_STATEMENTTYPE_PATTERN.matcher(matcher.group());
            	cMatcher1.find();
            	cMatcher2.find();
            	statement = cMatcher1.group(1);
            	statementType = cMatcher2.group(1);
            }
            String choiceId = UUID.randomUUID().toString();
            
    		// replace choice markup
    		setQhtml(getQhtml().replaceAll(matcher.group(), "<a href=\"" + WebStoryView.CHOICE_PROTO + choiceId + "\">" + text + "</a>"));

    		// add choices
            getChoices().add(ChoiceFactory.createChoice(choiceId, target, statement, ExecutableBase.toType(statementType)));
        }

        // replace resource urls
		setQhtml(getQhtml().replaceAll("qsr://", "content://net.kleinhenz.questor.qsr/"));
	}
}
